Shader "CF/GrayImageShader"
{
    Properties
    {
        _MainTex("Grayscale Texture", 2D) = "white" {}
    }
        SubShader
    {
        Tags { "RenderType" = "Opaque" "Queue" = "Transparent" }
        Lighting Off
        ZWrite Off
        Cull Off
        Fog { Mode Off }
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                float2 uv = i.uv;
                uv.y = 1.0 - uv.y; // 上下翻转（对齐 OpenCV）
                // 读取 alpha 或 red 通道，作为灰度值
                fixed4 tex = tex2D(_MainTex, uv);
                float gray = tex.a > 0.0 ? tex.a : tex.r;
                return fixed4(gray, gray, gray, 1.0);
            }
            ENDCG
        }
    }
}